import { defineConfig, loadEnv } from 'vite';
import * as path from 'path';
import vuePlugin from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')

  const alias = {
    '@': path.resolve(__dirname, './src'),
    'vue$': 'vue/dist/vue.runtime.esm-bundler.js',
  }

  return {
    base: env.VITE_WEB_PATH || '/',
    root: './',
    resolve: {
      alias,
    },
    define: {
      'process.env': {}
    },
    server: {
      open: '/',
      port: 8080,
      proxy: {
        '^/api/websocket': {
          target: 'ws://localhost:2001',
          ws: true,
          changeOrigin: true
        },
        '^/api/assets': {
          target: 'http://localhost:8080',
          changeOrigin: true,
          rewrite: path => path.replace('/api', '')
        },
        '^/api': {
          target: 'http://localhost:2001/',
          changeOrigin: true
        },
        '^/druid': {
          target: 'http://localhost:2001/api/',
          changeOrigin: true,
          cookiePathRewrite: { "/api": "" }
        },
      },
    },
    build: {
      target: 'es2017',
      minify: 'terser',
      manifest: false,
      sourcemap: false,
      emptyOutDir: true,
      chunkSizeWarningLimit: '2048kb',
      outDir: './dist',
      rollupOptions: {
        input: {
          index: path.resolve(__dirname, 'index.html'),
          choose: path.resolve(__dirname, 'choose.html'),
          business: path.resolve(__dirname, 'business.html'),
          admin: path.resolve(__dirname, 'admin.html'),
          sso: path.resolve(__dirname, 'sso.html'),
          portal: path.resolve(__dirname, 'portal.html'),
          mobile: path.resolve(__dirname, 'mobile.html'),
        },
        output: {
          chunkFileNames: 'assets/js/[name]-[hash].js',
          assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
          entryFileNames: 'assets/js/[name]-[hash].js',
        }
      }
    },
    plugins: [
      vuePlugin(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
        eslintrc: {
          enabled: true,
          filepath: ".eslintrc.json",
          globalsPropValue: true
        }
      }),
      Components({
        directoryAsNamespace: true,
        resolvers: [ElementPlusResolver()],
      })
    ],
    css: {
      preprocessorOptions: {
        scss: {
          api: "modern-compiler",
          silenceDeprecations: ['legacy-js-api'],
        }
      }
    },
  }
})
